package com.ruoyi.whzj.controller;

import java.io.IOException;
import java.util.List;

import com.ruoyi.common.config.Global;
import com.ruoyi.common.utils.EmptyUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.whzj.domain.WhVersion;
import com.ruoyi.whzj.service.IWhVersionService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 版本管理Controller
 * 
 * @author ruoyi
 * @date 2020-12-02
 */
@Controller
@RequestMapping("/whzj/version")
public class WhVersionController extends BaseController
{
    private String prefix = "whzj/version";

    @Autowired
    private IWhVersionService whVersionService;

    @RequiresPermissions("whzj:version:view")
    @GetMapping()
    public String version()
    {
        return prefix + "/version";
    }

    /**
     * 查询版本管理列表
     */
    @RequiresPermissions("whzj:version:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WhVersion whVersion)
    {
        startPage();
        List<WhVersion> list = whVersionService.selectWhVersionList(whVersion);
        return getDataTable(list);
    }

    /**
     * 导出版本管理列表
     */
    @RequiresPermissions("whzj:version:export")
    @Log(title = "版本管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WhVersion whVersion)
    {
        List<WhVersion> list = whVersionService.selectWhVersionList(whVersion);
        ExcelUtil<WhVersion> util = new ExcelUtil<WhVersion>(WhVersion.class);
        return util.exportExcel(list, "version");
    }

    /**
     * 新增版本管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存版本管理
     */
    @RequiresPermissions("whzj:version:add")
    @Log(title = "版本管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WhVersion whVersion)
    {
        return toAjax(whVersionService.insertWhVersion(whVersion));
    }

    /**
     * 修改版本管理
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        WhVersion whVersion = whVersionService.selectWhVersionById(id);
        mmap.put("whVersion", whVersion);
        return prefix + "/edit";
    }

    /**
     * 修改保存版本管理
     */
    @RequiresPermissions("whzj:version:edit")
    @Log(title = "版本管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(MultipartFile file, WhVersion whVersion) throws IOException {
        // 上传文件路径
        String filePath =Global.getUploadPath();
        // 上传并返回新文件名称
        if(EmptyUtils.isNotEmpty(file)){
            String fileName = FileUploadUtils.upload(filePath, file);
            whVersion.setVersionApk("http://whzj.hnsjjy.cn"+fileName);
        }
        return toAjax(whVersionService.updateWhVersion(whVersion));
    }

    /**
     * 删除版本管理
     */
    @RequiresPermissions("whzj:version:remove")
    @Log(title = "版本管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(whVersionService.deleteWhVersionByIds(ids));
    }
}
